//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer;
using CommunityServer.ControlPanel.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Reader.Components;

namespace CommunityServer.ControlPanel.Reader
{
	/// <summary>
	/// Summary description for AddAggregatedFeed.
	/// </summary>
	public class AddAggregatedFeed : BaseReaderAdminPage
	{
		protected CommunityServer.ControlPanel.Controls.ResourceLinkButton AddFeeds;
		protected CommunityServer.ControlPanel.Controls.ResourceLinkButton Done;
		protected ListBox lFeeds;
		protected Literal lStatus;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!this.IsPostBack)
			{
				LoadFeeds();	
				
			}
		}

		private void LoadFeeds()
		{
			ArrayList feeds = Feeds.GetUnaggregatedFeeds(Users.GetAnonymousUser().UserID);

			lFeeds.Items.Clear();
			for (int i = 0; i < feeds.Count; i++)
			{
				Feed feed = (Feed) feeds[i];

				string feedName = (feed.Title != null && feed.Title != "" ? feed.Title : "* - Unknown title") + " (" + feed.Url + ")";
				lFeeds.Items.Add(new ListItem(feedName, feed.FeedId.ToString()));

			}
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
			AddFeeds.Click += new EventHandler(AddFeeds_Click);
			Done.Click += new EventHandler(Done_Click);

		}
		#endregion

		private void AddFeeds_Click(object sender, EventArgs e)
		{
			int anonId = Users.GetAnonymousUser().UserID;


			for (int i = 0; i < lFeeds.Items.Count; i++)
			{
				if (lFeeds.Items[i].Selected)
				{
					Feeds.AddAggregatedFeed(anonId, Int32.Parse(lFeeds.Items[i].Value));
				}
			}

			lStatus.Text = "<br/>" + ResourceManager.GetString("CP_Reader_AggFeed_AddSuccessful") + "<br/>";
			LoadFeeds();
		}

		private void Done_Click(object sender, EventArgs e)
		{
			Response.Redirect("AggregatedFeeds.aspx");
		}
	}
}
